package com.itouch.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

/**
 * 日期处理类
 * @author PC1
 *
 */
public class DateUtils {

	public static boolean isAfter(String s1, String s2) {
		SimpleDateFormat dateSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		try {
			Date date1 = dateSdf.parse(s1);
			Date date2 = dateSdf.parse(s2);
			if (date1.after(date2)) {
				return true;
			} else {
				return false;
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 获取两日期时间差
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int getTimeDifference(Date c1, Date c2)
	{
		GregorianCalendar cal1 = new GregorianCalendar();  
		GregorianCalendar cal2 = new GregorianCalendar(); 
		cal1.setTime(c1);
		cal2.setTime(c2);
		double dayCount = (cal2.getTimeInMillis()-cal1.getTimeInMillis())/(1000*3600*24) + 1;
		return (int)dayCount;
	}

	public static boolean isBeforeDate(String s1, String s2) {
		SimpleDateFormat dateSdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date1 = dateSdf.parse(s1);
			Date date2 = dateSdf.parse(s2);
			if (date1.after(date2)) {
				return false;
			} else {
				return true;
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 获取当前日期
	 * @return 当前日期
	 */
	public static Date getCurrentTime() {
        TimeZone zone = TimeZone.getTimeZone("GMT+8"); 
        TimeZone.setDefault(zone);
		Date date = new Date(System.currentTimeMillis());
		return date;
	}

	/**
	 * Date对象转String类型
	 * @param date 日期
	 * @param formater 输出格式
	 * @return
	 */
	public static String getTimeStr(Date date, String formater)
	{
		TimeZone zone = TimeZone.getTimeZone("GMT+8"); 
        TimeZone.setDefault(zone);
        DateFormat df = new SimpleDateFormat(formater);  
        return df.format(date);
	}
	
	/**
	 * 日期计算函数，加上天数返回增加后的日期
	 * @param day 目标日期
	 * @param dayAddNum 增加的天数
	 * @return 增加后的日期
	 */
	public static String getDateStr(String day,int dayAddNum) {  
		TimeZone zone = TimeZone.getTimeZone("GMT+8"); 
        TimeZone.setDefault(zone);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");  
        Date nowDate = null;  
        try {  
            nowDate = df.parse(day);  
        } catch (ParseException e) {  
            e.printStackTrace();  
        }  
        
        Date newDate2 = new Date(nowDate.getTime() + dayAddNum * 24 * 60 * 60 * 1000);  
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");  
        String dateOk = simpleDateFormat.format(newDate2);  
        return dateOk;  
    }  
	
	/**
	 * 获取当前日期 String返回，格式  2014-01-01 13:21
	 * @return 当前时间
	 */
	public static String getCrrentTimeStr()   
	{  
        TimeZone zone = TimeZone.getTimeZone("GMT+8"); 
        TimeZone.setDefault(zone);

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");  
	    Date date = new Date();  
	    return df.format(date);  
	}  

	/**
	 * 格式化车辆预约返回的日期数据
	 * @param date 车辆预约中返回的日期
	 * @return 日期，格式为yyyy-MM-dd HH:mm
	 */
	@SuppressWarnings("deprecation")
	public static String formatOaCarTime(String date) {
		TimeZone zone = TimeZone.getTimeZone("GMT+8"); 
        TimeZone.setDefault(zone);
		int startIndex = date.indexOf("(");
		int endIndex = date.indexOf(")");
		if (startIndex == -1 || endIndex == -1) {
			return date;
		} else {
			SimpleDateFormat dateSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			String strDate = date.substring(startIndex + 1, endIndex);
			Date myDate = new Date(Long.parseLong(strDate));
			int iHour = myDate.getHours();
			myDate.setMonth(myDate.getMonth());
			myDate.setHours(iHour);
			return dateSdf.format(myDate);

		}
	}
	
	/**
	 * 格式化日期，String转Date类
	 * @param date 日期
	 * @param format 日期显示类型
	 * @return 格式化后Date 类
	 * @throws ParseException 解析异常
	 */
	public static Date formateDate(String date, String format) throws ParseException
	{
		SimpleDateFormat dateSdf = new SimpleDateFormat(format);
		return dateSdf.parse(date);  
	}

	/**
	 * 根据日期来获取该日期为星期几
	 * @param pTime 日期
	 * @return 星期几
	 */
	public static String getWeekForDuty(String pTime) {

		String Week = "星期";

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		try {

			c.setTime(format.parse(pTime));

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (c.get(Calendar.DAY_OF_WEEK) == 1) {
			Week += "天";
		}
		if (c.get(Calendar.DAY_OF_WEEK) == 2) {
			Week += "一";
		}
		if (c.get(Calendar.DAY_OF_WEEK) == 3) {
			Week += "二";
		}
		if (c.get(Calendar.DAY_OF_WEEK) == 4) {
			Week += "三";
		}
		if (c.get(Calendar.DAY_OF_WEEK) == 5) {
			Week += "四";
		}
		if (c.get(Calendar.DAY_OF_WEEK) == 6) {
			Week += "五";
		}
		if (c.get(Calendar.DAY_OF_WEEK) == 7) {
			Week += "六";
		}

		return Week;
	}
	
	

}
